const CustomKoa = require("./custom-koa");
const Router = require("./router");
const app = new CustomKoa();
const router = new Router();

// 浅层封装
// app.use((req, res) => {
//     res.writeHead(200);
//     res.end("hello CustomKoa");
// })

// app.use(ctx => {
//     ctx.body = "haha";
// })

// const delay = () => Promise.resolve(resolve => {
//     setTimeout(() => resolve(), 2000);
// })
// app.use(async (ctx, next) => {
//     ctx.body = "1";
//     console.log(ctx.body);
//     await next();
//     ctx.body += "5";
//     console.log(ctx.body);
// })

// app.use(async (ctx, next) => {
//     ctx.body += "2";
//     console.log(ctx.body);
//     await delay();
//     await next();
//     ctx.body += "4";
//     console.log(ctx.body);
// })

// app.use(async (ctx, next) => {
//     ctx.body += "3";
//     console.log(ctx.body);
// })

router.get("/", async ctx => { ctx.body = "index page"; console.log("index page");});
router.get("/list", async ctx => {ctx.body = "list page"});
router.get("/post", async ctx => {ctx.body = "post page"});
router.post("/post", async ctx => {ctx.body = "post post page"});

app.use(router.routes());

const static = require("./static");
app.use(static(__dirname + "/public"));

app.listen(3000, () => {
    console.log("app lieten 3000");
})